package com.example.express1222;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.express1222.api.KdSearch;
import com.example.express1222.entity.BaseData;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    List<BaseData.Trace>data=new ArrayList<>(); /*定义的data*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lv=findViewById(R.id.lv);

        MyAdapter adapter =new MyAdapter() ;
        lv.setAdapter(adapter);


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api09.aliyun.venuscn.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        KdSearch search = retrofit.create(KdSearch.class);
        Call<BaseData> kd = search.getKD("SF1980543358862\n", "");
        kd.enqueue(new Callback<BaseData>() {//YT9415769187782 75422961880316

            @Override
            public void onResponse(Call<BaseData> call, Response<BaseData> response) {
                Log.e("-----success--->", response.body().getData().getTraces().get(0).getDesc());
                            /**/
                List<BaseData.Trace>traces =response.body().getData().getTraces() ;
                data=traces ;  //接受数据
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onFailure(Call<BaseData> call, Throwable t) {
                Log.e("--failed-->", t.getMessage());
            }
        });
    }


    /*先做ListView 在做数据*/
    class  MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {

            if (view== null){
                view =getLayoutInflater().inflate(R.layout.cell,null);

            }

            TextView msg=view.findViewById(R.id.msg);
            TextView date=view.findViewById(R.id.date) ;
            BaseData.Trace trace =data.get(position) ;
            msg.setText(trace.getDesc());
            date.setText(trace.getTime());
            return view ;


        }
    }



}
